import { _decorator, Animation, AnimationClip, Component, easing, Label, Node, Sprite, Tween, tween, Vec3 } from 'cc';
import { AssetList, Global } from '../Global';
import { ResMgr } from '../Mgr/ResMgr';
import { GameScene } from './GameScene';
import { ModelLoad } from './ModelLoad';
import { AddScoreLoad } from './AddScoreLoad';
const { ccclass, property } = _decorator;

export type CubeInfo = {
    hor: number,
    ver: number,
    ele: number
}

@ccclass('SingleCubeBg')
export class SingleCubeBg extends Component {

    //测试文字
    @property(Label)
    public TestContent: Label = null;

    //方块信息
    public CubeInfo: CubeInfo = { hor: 0, ver: 0, ele: 0 }

    //元素内容
    @property(Node)
    private Ele: Node = null;

    //元素原位置
    public StarPos: Vec3 = new Vec3(-19, 22);
    //落点位置
    public EndPos: Vec3 = new Vec3(-73, -38);

    /**
     * 更新
     */
    public InitCubeInfor(_cubeinfor: CubeInfo) {
        this.CubeInfo = _cubeinfor;
        this.Ele.active = false;
    }

    /**
     * 清除
     */
    public Clear() {
        this.CubeInfo.ele = -1;
        this.Ele.active = false;
    }

    /**
     * 添加元素
     */
    public AddEle(_ele: number) {
        this.CubeInfo.ele = _ele;
        this.Ele.active = true;
        this.Ele.getComponent(Sprite).spriteFrame = ResMgr.Instance.SpriteFrames[AssetList.SpriteFrame.Ele + _ele.toString()]
        //因为播放动画位置不对了所以初始位置
        this.Ele.parent.scale = new Vec3(1, 1, 1);
    }

    /**
     * 加分
     */
    public AddScore(_score: number) {
        GameScene.Instance.ModelLoad.getComponent(ModelLoad).UpdateScore(_score);
        GameScene.Instance.AddScoreLoad.getComponent(AddScoreLoad).AddAddScore(_score, this.node.getWorldPosition());
        this.PlayDis();
    }

    /**
     * 播放消失动画
     */
    private PlayDis() {
        tween(this.Ele.parent).to(0.3, { scale: new Vec3(0.5, 0.5) }, { easing: easing.backIn }).call(this.Clear.bind(this)).start();
    }


}


